Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Class variable</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Class_variable"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Class_variable rootpage-Class_variable skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Class variable</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Class-based" class="mw-redirect" title="Class-based">class-based</a>, <a href="Object-oriented_programming" title="Object-oriented programming">object-oriented programming</a>, a <b>class variable</b> is a <a href="Variable_(programming)" class="mw-redirect" title="Variable (programming)">variable</a> defined in a <a href="Class_(computer_programming)" title="Class (computer programming)">class</a> of which a single copy exists, regardless of how many <a href="Instance_(computer_science)" title="Instance (computer science)">instances</a> of the class exist.<sup id="cite_ref-TheJavaTurotialVariables_1-0" class="reference"><a href="#cite_note-TheJavaTurotialVariables-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-TheJavaTutorialUnderstandingInstanceAndClassMembers_2-0" class="reference"><a href="#cite_note-TheJavaTutorialUnderstandingInstanceAndClassMembers-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-ThePythonLanguageReferenceCompoundStatements_3-0" class="reference"><a href="#cite_note-ThePythonLanguageReferenceCompoundStatements-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-ObjectiveCRuntimeReference_4-0" class="reference"><a href="#cite_note-ObjectiveCRuntimeReference-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-ClassVariablesinCSharp_5-0" class="reference"><a href="#cite_note-ClassVariablesinCSharp-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup>
</p><p>A class variable is not an <a href="Instance_variable" title="Instance variable">instance variable</a>. It is a special type of <a href="Class_(computer_science)" class="mw-redirect" title="Class (computer science)">class attribute</a> (or class property, <a href="Field_(computer_science)" title="Field (computer science)">field</a>, or data member). The same dichotomy between <i>instance</i> and <i>class</i> members applies to <a href="Method_(computer_science)" class="mw-redirect" title="Method (computer science)">methods</a> ("member functions") as well; a class may have both <a href="Instance_method" class="mw-redirect" title="Instance method">instance methods</a> and <a href="Class_method" class="mw-redirect" title="Class method">class methods</a>.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Static_member_variables_and_static_member_functions">Static member variables and static member functions</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">See also: <a href="Static_method" class="mw-redirect" title="Static method">Static method</a></div>
<p>In some languages, class variables and class methods are either statically resolved, not via <a href="Dynamic_dispatch" title="Dynamic dispatch">dynamic dispatch</a>, or their memory <a href="Static_memory_allocation" class="mw-redirect" title="Static memory allocation">statically allocated</a> at compile time (once for the entire class, as <a href="Static_variable" title="Static variable">static variables</a>), not dynamically allocated at run time (at every instantiation of an object). In other cases, however, either or both of these are dynamic. For example, if classes can be dynamically defined (at run time), class variables of these classes are allocated dynamically when the class is defined, and in some languages class methods are also dispatched dynamically.
</p><p>Thus in some languages, <b>static member variable</b> or <b>static member function</b> are used synonymously with or in place of "class variable" or "class function", but these are not synonymous across languages. These terms are commonly used in <a href="Java_(programming_language)" title="Java (programming language)">Java</a>, <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>,<sup id="cite_ref-ClassVariablesinCSharp_5-1" class="reference"><a href="#cite_note-ClassVariablesinCSharp-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> and <a href="C%2B%2B" title="C++">C++</a>, where class variables and class methods are declared with the <a href="Static_(keyword)" title="Static (keyword)"><code>static</code> keyword</a>, and referred to as <b>static member variables</b> or <b>static member functions</b>.
</p>
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<div class="mw-heading mw-heading3"><h3 id="C++">C++</h3></div>
<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr mw-highlight-lines" dir="ltr"><pre><span class="k">struct</span><span class="w"> </span><span class="nc">Request</span><span class="w"> </span><span class="p">{</span>

<span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">number</span><span class="p">;</span>

<span class="w"> </span><span class="n">Requestobject</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">number</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">count</span><span class="p">;</span><span class="w"> </span><span class="c1">// modifies the instance variable "this-&gt;number"</span>
<span class="w"> </span><span class="o">++</span><span class="n">count</span><span class="p">;</span><span class="w"> </span><span class="c1">// modifies the class variable "Request::count"</span>
<span class="w"> </span><span class="p">}</span>

<span class="p">};</span>

<span class="kt">int</span><span class="w"> </span><span class="n">Request</span><span class="o">::</span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
</pre></div>
<p>In this C++ example, the class variable <code>Request::count</code> is <a href="Increment_operator" class="mw-redirect" title="Increment operator">incremented</a> on each call to the <a href="Constructor_(computer_science)" class="mw-redirect" title="Constructor (computer science)">constructor</a>, so that <code>Request::count</code> always holds the number of Requests that have been constructed, and each new Request object is given a <code>number</code> in sequential order. Since <code>count</code> is a class variable, there is only one object <code>Request::count</code>; in contrast, each Request object contains its own distinct <code>number</code> field.
</p><p>Also note that the variable <code>Request::count</code> is initialized only once.
</p>
<div class="mw-heading mw-heading3"><h3 id="Python">Python</h3></div>
<div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="nc">Dog</span><span class="p">:</span>
<span class="n">vertebrate_group</span> <span class="o">=</span> <span class="s1">'mammals'</span> <span class="c1"># class variable</span>

<span class="n">dog_1</span> <span class="o">=</span> <span class="n">Dog</span>
<span class="nb">print</span><span class="p">(</span><span class="n">dog_1</span><span class="o">.</span><span class="n">vertebrate_group</span><span class="p">)</span> <span class="c1"># accessing the class variable</span>
</pre></div><p>In the above Python code, it does not provide much information as there is only class variable in the Dog class that provide the vertebrate group of dog as mammals. In instance variable, you could customize your own object (in this case, dog_1) by having one or more <a href="Instance_variable" title="Instance variable">instance variables</a> in the Dog class.
</p><div class="mw-heading mw-heading2"><h2 id="Notes">Notes</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist reflist-columns references-column-width reflist-columns-2">
<ol class="references">
<li id="cite_note-TheJavaTurotialVariables-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-TheJavaTurotialVariables_1-0">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html">"The Java Tutorial, Variables"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2010-10-21</span></span>.</cite></span>
</li>
<li id="cite_note-TheJavaTutorialUnderstandingInstanceAndClassMembers-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-TheJavaTutorialUnderstandingInstanceAndClassMembers_2-0">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html">"The Java Tutorial, Understanding Instance and Class Members"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2010-10-21</span></span>.</cite></span>
</li>
<li id="cite_note-ThePythonLanguageReferenceCompoundStatements-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-ThePythonLanguageReferenceCompoundStatements_3-0">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.python.org/reference/compound_stmts.html#class-definitions">"The Python Language Reference, Compound Statements"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2010-10-21</span></span>.</cite></span>
</li>
<li id="cite_note-ObjectiveCRuntimeReference-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-ObjectiveCRuntimeReference_4-0">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://developer.apple.com/documentation/objectivec/objective_c_runtime">"Objective-C Runtime Reference"</a>. <i>Apple Developer</i><span class="reference-accessdate">. Retrieved <span class="nowrap">1 April</span> 2018</span>.</cite></span>
</li>
<li id="cite_note-ClassVariablesinCSharp-5"><span class="mw-cite-backlink">^ <a href="#cite_ref-ClassVariablesinCSharp_5-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-ClassVariablesinCSharp_5-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://syntaxdb.com/ref/csharp/class-var">"Class Variables in C#"</a>. <i>Syntaxdb</i><span class="reference-accessdate">. Retrieved <span class="nowrap">1 April</span> 2018</span>.</cite></span>
</li>
</ol></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-01-13" href="https://en.wikipedia.org/wiki/?title=Class_variable&amp;oldid=1269130187">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>